package encoder

import (
	"bytes"
	"encoding/base64"
	"encoding/hex"
	"strings"
	"testing"
)

var (
	rawByte          = []byte("Hello, world!")
	base64EncodedStr = "SGVsbG8sIHdvcmxkIQ=="
	hexEncodedStr    = "48656c6c6f2c20776f726c6421"
)

func TestBase64Encoder_Encode(t *testing.T) {
	dst := make([]byte, base64.StdEncoding.EncodedLen(len(rawByte)))
	Base64Encoder.Encode(dst, rawByte)

	if !strings.EqualFold(string(dst), base64EncodedStr) {
		t.Error("BASE64编码失败")
	}
}

func TestBase64Encoder_EncodeToString(t *testing.T) {
	encoded := Base64Encoder.EncodeToString(rawByte)

	if !strings.EqualFold(encoded, base64EncodedStr) {
		t.Error("BASE64编码失败")
	}
}

func TestBase64Encoder_Decode(t *testing.T) {
	dst, err := Base64Encoder.Decode([]byte(base64EncodedStr))
	if err != nil {
		t.Error(err)
	}

	if !bytes.EqualFold(dst, rawByte) {
		t.Error("BASE64解码失败")
	}
}

func TestBase64Encoder_DecodeString(t *testing.T) {
	dst, err := Base64Encoder.DecodeString(base64EncodedStr)
	if err != nil {
		t.Error(err)
	}

	if !bytes.EqualFold(dst, rawByte) {
		t.Error("BASE64解码失败")
	}
}

func TestHexEncoder_Encode(t *testing.T) {
	dst := make([]byte, hex.EncodedLen(len(rawByte)))
	HexEncoder.Encode(dst, rawByte)

	if !strings.EqualFold(string(dst), hexEncodedStr) {
		t.Error("Hex编码失败")
	}
}

func TestHexEncoder_EncodeToString(t *testing.T) {
	dst := HexEncoder.EncodeToString(rawByte)

	if !strings.EqualFold(dst, hexEncodedStr) {
		t.Error("Hex编码失败")
	}
}

func TestHexEncoder_Decode(t *testing.T) {
	dst, err := HexEncoder.Decode([]byte(hexEncodedStr))
	if err != nil {
		t.Error(err)
	}

	if !bytes.EqualFold(dst, rawByte) {
		t.Error("Hex解码失败")
	}
}

func TestHexEncoder_DecodeString(t *testing.T) {
	dst, err := HexEncoder.DecodeString(hexEncodedStr)
	if err != nil {
		t.Error(err)
	}

	if !bytes.EqualFold(dst, rawByte) {
		t.Error("Hex解码失败")
	}
}
